<!DOCTYPE html>
<html lang="en">
<head>
    <!--
    @Author 硕鼠
    @Date 2020/4/7 8:43
    @Version 1.0
    @Description 对于NAN的测试、显式转换
  -->
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        window.onload = function () {
            var oTxt1 = document.getElementById('txt1');
            var oTxt2 = document.getElementById('txt2');
            var oBut = document.getElementById('but1');

            oBut.onclick = function () {

                //强制类型转换（显式类型）
                var n1 = parseInt(oTxt1.value); //parseInt:对整数进行转化
                var n2 = parseInt(oTxt2.value); //parseFlaot:对小数进行转化
                if (isNaN(n1)){
                    alert('你输入的第一个数字有误');
                }else if (isNaN(n2)){
                    alert('你输入的第二个数字有误');
                }else {
                    alert(n1 + n2);
                }
            }
        };
    </script>
</head>
<body>

<input id="txt1" type="text">
<input id="txt2" type="text">
<input id="but1" type="button" value="求和">
</body>
</html>